package com.gsn.chooseRoom;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.MakeRoomModel;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.GsnException;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.name.NameEdit;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class MakeRoomLayer extends GsnLayer implements ClickListener {
	private static final String tag = "DialogLayer";
	float time = 0.5f;
	protected ParticleEffect clickEff;
	public int id;
	Label labelGold;
	Label labelXu;
	GsnImageButton agree;
	GsnImageButton cancel;
	GsnImageButton hard;
	GsnImageButton easy;
	GsnImageButton drop;
	public ChooseBet chooseBet;
	public boolean init = false;
	Image imageBet;
	public Label labelBetNumber;

	public String title[] = { "Thùng phá sành", "Nhiều tiền thì vào", "Cây bài tẩy", "Chơi tới bến" };

	public MakeRoomLayer(float width, float height) {
		super(width, height);

		PortalTexture portalTexture = PortalTexture.inst();
		FontTexture fontTexture = FontTexture.inst();

		Image bgDark = new Image(PortalTexture.inst().bgDarkDialog);
		PortalTexture.inst().scaleActor(bgDark);
		addActor(bgDark);
		bgDark.width = width;
		bgDark.height = height;

		Image bg = new Image(PortalTexture.inst().bgDialog);
		PortalTexture.inst().scaleActor(bg);
		addActor(bg);
		bg.x = (width - bg.width) / 2;
		bg.y = (height - bg.height) / 2;
		float pad = width * 0.01f;
		Label labelTitle = new Label("Tạo bàn", new Label.LabelStyle(fontTexture.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelTitle);
		labelTitle.x = bg.x + bg.width * 0.05f;
		labelTitle.y = bg.y + bg.height * 0.92f;

		Label labelName = new Label("Tên bàn:", new Label.LabelStyle(fontTexture.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelName);
		labelName.x = bg.x + bg.width * 0.1f;
		labelName.y = bg.y + bg.height * 0.7f;

		Image imageName = new Image(PortalTexture.inst().textBoxWhite);
		PortalTexture.inst().scaleActor(imageName);
		addActor(imageName);
		imageName.width = bg.width * 0.4f;
		imageName.x = bg.x + bg.width * 0.35f;
		imageName.y = labelName.y;

		int rand = (int) (Math.random() * 4 - 0.01);

		UtilityGettor.inst().makeTextFieldWithScreenCoordinate(this, PortalTexture.inst().skin.getStyle(TextField.TextFieldStyle.class), NameEdit.MAKE_ROOM, title[rand], imageName.width, imageName.height, height - imageName.y - imageName.height, imageName.x);
		UtilityGettor.inst().hideTextField(NameEdit.MAKE_ROOM);

		Label labelBet = new Label("Gold cược:", new Label.LabelStyle(fontTexture.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelBet);
		labelBet.x = labelName.x;
		labelBet.y = bg.y + bg.height * 0.6f;

		imageBet = new Image(PortalTexture.inst().textBoxWhite);
		PortalTexture.inst().scaleActor(imageBet);
		addActor(imageBet);
		imageBet.width = width * 0.2f;
		imageBet.x = imageName.x;
		imageBet.y = labelBet.y;

		labelBetNumber = new Label("fdfsf", new Label.LabelStyle(fontTexture.fontMedium, new Color(0, 0, 0, 1)));
		addActor(labelBetNumber);
		labelBetNumber.x = imageBet.x + imageBet.width * 0.05f;
		labelBetNumber.y = labelBet.y;

		easy = new GsnImageButton(PortalTexture.inst().checkButton, PortalTexture.inst().checkButton, PortalTexture.inst().checkButtonPress);
		PortalTexture.inst().scaleActor(easy);
		addActor(easy);
		easy.x = labelName.x;
		easy.y = bg.y + bg.height * 0.4f;
		easy.setClickListener(this);

		Label labelEasy = new Label("Tập sự", new Label.LabelStyle(fontTexture.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelEasy);
		labelEasy.x = easy.x + easy.getRealWidth() * 1.1f;
		labelEasy.y = easy.y;

		hard = new GsnImageButton(PortalTexture.inst().checkButton, PortalTexture.inst().checkButton, PortalTexture.inst().checkButtonPress);
		PortalTexture.inst().scaleActor(hard);
		addActor(hard);
		hard.x = bg.x + bg.width * 0.6f;
		hard.y = easy.y;
		hard.setClickListener(this);

		Label labelHard = new Label("Nâng cao", new Label.LabelStyle(fontTexture.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelHard);
		labelHard.x = hard.x + hard.getRealWidth() * 1.1f;
		labelHard.y = hard.y;

		agree = new GsnImageButton(PortalTexture.inst().agreeButton, PortalTexture.inst().agreeButtonPress);
		PortalTexture.inst().scaleActor(agree);
		addActor(agree);
		agree.x = bg.x + bg.width * 0.2f;
		agree.y = bg.y + bg.height * 0.1f;
		agree.setClickListener(this);

		cancel = new GsnImageButton(PortalTexture.inst().cancelButton, PortalTexture.inst().cancelButtonPress);
		PortalTexture.inst().scaleActor(cancel);
		addActor(cancel);
		cancel.x = bg.x + bg.width * 0.8f - cancel.getRealWidth();
		cancel.y = bg.y + bg.height * 0.1f;
		cancel.setClickListener(this);

		drop = new GsnImageButton(PortalTexture.inst().iconDropDialog, PortalTexture.inst().iconDropDialogPress);
		PortalTexture.inst().scaleActor(drop);
		addActor(drop);
		drop.x = imageBet.x + imageBet.width - drop.getRealWidth();
		drop.y = imageBet.y + (imageBet.height - drop.getRealHeight()) / 2;
		drop.setClickListener(this);

		clickEff = PortalTexture.inst().clickEff;

	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub

		if (clickEff != null) {
			Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);
			GsnParticleActor par = new GsnParticleActor(clickEff);
			par.setPostition(v.x, v.y);
			par.start();
			this.addActor(par);
		}
		PokerSound.playClick();
		if (actor == agree) {
			// MyPoker.client.send(PacketFactory.shopGold(id));

			// if (UtilityGettor.onMobile)
			MakeRoomModel.name = UtilityGettor.inst().getString(NameEdit.MAKE_ROOM);
			// else
			// MakeRoomModel.name = labelNameField.getText();

			if (!checkName(MakeRoomModel.name)) {
				MyPoker.game.chooseRoomScreen.showDialog("Bạn phải nhập tên bàn");
				return;
			}
			hide();
			if (hard.isChecked())
				MakeRoomModel.type = 1;
			else
				MakeRoomModel.type = 0;
			MyPoker.game.statePortal = StatePortal.CHOOSING_ROOM;

			try {
				MyPoker.game.playScreen.finishInit();
			} catch (GsnException ex) {
				ex.printStackTrace();
			}
			MyPoker.game.chooseRoomScreen.setDefaultInputListener();
			UtilityGettor.inst().showProgressDialog("Tạo bàn", "Đang tạo bàn");
			MyPoker.client.send(PacketFactory.makeARoom());

		} else if (actor == cancel) {
			hide();
		} else if (actor == drop) {
			if (!chooseBet.visible) {
				chooseBet.show();
				chooseBet.y = imageBet.y - chooseBet.height;
			} else {
				chooseBet.visible = false;
			}
		} else if (actor == hard) {

			easy.setChecked(!easy.isChecked());
		} else if (actor == easy) {

			hard.setChecked(!hard.isChecked());
		}

	}

	public InputProcessor oldInput;

	public void show() {
		init();
		hard.setChecked(true);
		easy.setChecked(false);
		labelBetNumber.setText(String.valueOf(getBet()));
		UtilityGettor.inst().visibleTextField(NameEdit.MAKE_ROOM);
		MakeRoomModel.bet = getBet();
		oldInput = Gdx.input.getInputProcessor();

		visible = true;
		setInputListener();
	}

	public void hide() {
		Gdx.input.setInputProcessor(oldInput);
		UtilityGettor.inst().hideTextField(NameEdit.MAKE_ROOM);
		oldInput = null;
		visible = false;
	}

	@Override
	public boolean keyDown(int keycode) {
		switch (keycode) {
		case Keys.A:
			for (GsnLayer l : MyPoker.game.chooseChannelScreen.layers) {
				System.out.println(l.getClass().getName());
			}
			break;
		}
		return super.keyDown(keycode);
	}

	public void init() {
		if (init)
			return;
		init = true;
		chooseBet = new ChooseBet(0.2f * width, this);
		addActor(chooseBet);
		chooseBet.hide();
		chooseBet.x = imageBet.x;

	}

	public boolean checkName(String name) {
		for (int i = 0; i < name.length(); i++) {
			if (name.charAt(i) != ' ')
				return true;
		}
		return false;
	}

	public int getBet() {
		int i;
		for (i = ConfigModel.channelModelList[ConfigModel.selectedChannel].bets.length - 1; i >= 0; i--) {
			if (3 * ConfigModel.channelModelList[ConfigModel.selectedChannel].bets[i] <= UserData.gold) {
				return ConfigModel.channelModelList[ConfigModel.selectedChannel].bets[i];
			}
		}
		return 0;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		if (!super.touchUp(x, y, pointer, button)) {
			chooseBet.hide();
		}
		return super.touchUp(x, y, pointer, button);
	}
}
